using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesToolsEntertainmentForGift : System.Web.UI.Page
{
    #region Variables

    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;
    protected EntertainmentHeader ResultObj = null;
    protected readonly int TotalColumns = 6;
    protected readonly int DefaultRowNumber = 2;
    protected bool ReadOnly = false;
    protected int CurRow = 1;

    #region EntertainmentID

    protected string EntertainmentID
    {
        get
        {
            return ViewState["EntertainmentID"].ToString();
        }

        set
        {
            ViewState["EntertainmentID"] = value;
        }
    }

    #endregion

    #region ClaimItemId
    protected string ClaimItemId
    {
        get
        {
            return ViewState["ClaimItemId"].ToString();
        }

        set
        {
            ViewState["ClaimItemId"] = value;
        }
    }
    #endregion

    #region ClaimInternalID
    protected string ClaimInternalID
    {
        get
        {
            return ViewState["ClaimInternalID"].ToString();
        }

        set
        {
            ViewState["ClaimInternalID"] = value;
        }
    }
    #endregion

    #region Components for Claim Detail Table Grid

    protected int ClaimDetailRowBuiltCounter = 0;   // everytime will be 0

    #region ActualNumberOfRow
    protected int ActualNumberOfRow
    {
        get
        {
            return (int)ViewState["ActualNumberOfRow"];
        }

        set
        {
            ViewState["ActualNumberOfRow"] = value;
        }
    }
    #endregion

    #region ClaimDetailIDList
    protected ArrayList ClaimDetailIDList
    {
        get
        {
            return (ArrayList)ViewState["ClaimDetailIDList"];
        }

        set
        {
            ViewState["ClaimDetailIDList"] = value;
        }
    }
    #endregion

    #endregion

    #region Common Variables for Language Translation

    #region ValidationText
    protected string ValidationText
    {
        get
        {
            //return ViewState["ValidationText"].ToString();
            string text = (string)ViewState["ValidationText"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }

        set
        {
            ViewState["ValidationText"] = value;
        }
    }
    #endregion

    #region MandatoryMessage
    protected string MandatoryMessage
    {
        get
        {
            //return ViewState["MandatoryMessage"].ToString();
            string text = (string)ViewState["MandatoryMessage"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }

        set
        {
            ViewState["MandatoryMessage"] = value;
        }
    }
    #endregion

    #region NonNegativeMessage
    protected string NonNegativeMessage
    {
        get
        {
            //return ViewState["NonNegativeMessage"].ToString();
            string text = (string)ViewState["NonNegativeMessage"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }

        set
        {
            ViewState["NonNegativeMessage"] = value;
        }
    }
    #endregion

    #region WarningMessage
    protected string WarningMessage
    {
        get
        {
            //return ViewState["WarningMessage"].ToString();
            string text = (string)ViewState["WarningMessage"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }

        set
        {
            ViewState["WarningMessage"] = value;
        }
    }
    #endregion

    #region NameListCopied
    protected string NameListCopied
    {
        get
        {
            //return ViewState["NameListCopied"].ToString();
            string text = (string)ViewState["NameListCopied"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }

        set
        {
            ViewState["NameListCopied"] = value;
        }
    }
    #endregion

    #region DataSaved
    protected string DataSaved
    {
        get
        {
            //return ViewState["DataSaved"].ToString();
            string text = (string)ViewState["DataSaved"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }

        set
        {
            ViewState["DataSaved"] = value;
        }
    }
    #endregion

    #region ConfirmedMessage
    protected string ConfirmedMessage
    {
        get
        {
            //return ViewState["ConfirmedMessage"].ToString();
            string text = (string)ViewState["ConfirmedMessage"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }

        set
        {
            ViewState["ConfirmedMessage"] = value;
        }
    }
    #endregion

    #region Number
    protected string Number
    {
        get
        {
            //return ViewState["Number"].ToString();
            string text = (string)ViewState["Number"];
            if (text != null)
                return text;
            else
                return string.Empty;

        }

        set
        {
            ViewState["Number"] = value;
        }
    }
    #endregion

    #region Participants
    protected string Participants
    {
        get
        {
            //return ViewState["Participants"].ToString();
            string text = (string)ViewState["Participants"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }

        set
        {
            ViewState["Participants"] = value;
        }
    }
    #endregion

    #region Titles
    protected string Titles
    {
        get
        {
            //return ViewState["Titles"].ToString();
            string text = (string)ViewState["Titles"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }

        set
        {
            ViewState["Titles"] = value;
        }
    }
    #endregion

    #region Affiliates
    protected string Affiliates
    {
        get
        {
            //return ViewState["Affiliates"].ToString();
            string text = (string)ViewState["Affiliates"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }

        set
        {
            ViewState["Affiliates"] = value;
        }
    }
    #endregion

    #region CostPerHead
    protected string CostPerHead
    {
        get
        {
            //return ViewState["CostPerHead"].ToString();
            string text = (string)ViewState["CostPerHead"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }

        set
        {
            ViewState["CostPerHead"] = value;
        }
    }
    #endregion

    #region ErrorMessage
    protected string ErrorMessage
    {
        get
        {
            //return ViewState["ErrorMessage"].ToString();
            string text = (string)ViewState["ErrorMessage"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }

        set
        {
            ViewState["ErrorMessage"] = value;
        }
    }
    #endregion

    #endregion

    #endregion

    #region Events

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();
        this.SwitchLanguage();
        this.BuiltTable();

        if (!IsPostBack)
        {
            #region Claim ID and Claim detail id

            if (Request.QueryString["id"] != null && Request.QueryString["id"].Length == 36 &&
                Request.QueryString["did"] != null && Request.QueryString["did"].Length == 36)
            {
                this.ClaimInternalID = Request.QueryString["id"];
                this.ClaimItemId = Request.QueryString["did"];
            }
            else
            {
                // redirect 
            }

            #endregion

            #region NoEdit
            if (Request.QueryString["NoEdit"] != null && Request.QueryString["NoEdit"].CompareTo("1") == 0)
            {
                this.ReadOnly = true;
            }
            #endregion

            #region Auto-fill default data

            string Remarks = Request.QueryString["remarks"];
            string AmtInStr = Request.QueryString["amt"];

            if (Remarks.Length > 0 && Remarks.IndexOf(",") > 0)
            {
                string[] RemarksArray = SwissArmy.StringSplit(Remarks, ",");
                //this.TextBox_Purpose.Text = RemarksArray[0].Replace("[ENT]", "");
                this.TextBox_NoOfParticipants.Text = RemarksArray[1];
            }

            if (AmtInStr != null && AmtInStr.Length > 0)
            {
                //this.TextBox_TotalCost.Text = AmtInStr;
            }
            #endregion

            this.PageComponentSetup();
            //this.SwitchLanguage();

            this.LoadData();

            this.ClaimDetailIDList = new ArrayList();

            if (this.ResultObj != null)
            {
                int RowNeeded = (this.ResultObj.Details.Count > this.DefaultRowNumber) ? 0 : (this.DefaultRowNumber - this.ResultObj.Details.Count);
                int TotalRowToBuilt = RowNeeded + this.ResultObj.Details.Count;

                for (int x = 0; x < TotalRowToBuilt; x++)
                {
                    if (x < this.ResultObj.Details.Count)
                    {
                        this.BuildRow(this.ResultObj.Details[x].InternalID);
                    }
                    else
                    {
                        this.BuildRow();
                    }
                }

                this.FillData();
                this.LoadIntoDetailGrid();
            }
            else
            {
                for (int x = 0; x < this.DefaultRowNumber; x++)
                {
                    this.BuildRow();
                }
            }

        }
        else
        {
            // this is a post back
            for (int x = 0; x < this.ClaimDetailIDList.Count; x++)
            {
                this.BuildRow(this.ClaimDetailIDList[x].ToString());
            }
        }

        this.GenerateWarningMessage();
    }
    #endregion

    #region Button_AddMoreRow_Click
    protected void Button_AddMoreRow_Click(object sender, EventArgs e)
    {

        for (int x = 0; x < 10; x++)
        {
            this.BuildRow();
        }
    }
    #endregion

    #region Button_Submit_Click
    protected void Button_Submit_Click(object sender, EventArgs e)
    {
        this.DataCollect();

        if (this.DataSave())
        {
            //this.TextBox_NoOfParticipants.Text = (this.RadioButton_EnterNoOfParticpants.Checked) ? this.TextBox_NoOfParticipants.Text : this.HiddenField_TotalParticipants.Value = this.ResultObj.Details.Count.ToString();
            if (this.ResultObj.Details.Count > 0)
                this.TextBox_NoOfParticipants.Text = this.ResultObj.Details.Count.ToString();
            //else
            //    this.TextBox_NoOfParticipants.Text = this.HiddenField_TotalParticipants.Value;

            this.TextBox_TotalCost.Text = this.ResultObj.TotalCost.ToString();
            this.ShowMessage(this.DataSaved);
        }
    }
    #endregion

    #region Button_Done_Click
    protected void Button_Done_Click(object sender, EventArgs e)
    {
        this.DataCollect();

        if (this.DataSave())
        {
            if (this.ResultObj.Details.Count > 0)
                this.TextBox_NoOfParticipants.Text = this.ResultObj.Details.Count.ToString();

            //this.HiddenField_TotalParticipants.Value = this.ResultObj.Details.Count.ToString();
            this.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "Close", "<script language='javascript'>parent.ModalWindow.hide();</script>");
        }
    }
    #endregion

    #region Button_Done_Click1
    protected void Button_Done_Click1(object sender, EventArgs e)
    {
        this.DataCollect();

        if (this.DataSave())
        {
            if (this.ResultObj.Details.Count > 0)
                this.TextBox_NoOfParticipants.Text = this.ResultObj.Details.Count.ToString();

            //this.HiddenField_TotalParticipants.Value = this.ResultObj.Details.Count.ToString();
            this.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "Close", "<script language='javascript'>parent.ModalWindow.hide();</script>");
        }

    }
    #endregion

    #region Button_Remove_Click
    protected void Button_Remove_Click(object sender, EventArgs e)
    {
        int x = 1;

        while (x < (this.Table_ClaimDetailGrid.Rows.Count))
        {
            if (((CheckBox)this.Table_ClaimDetailGrid.Rows[x].Cells[0].Controls[0]).Checked)
            {
                for (int y = 0; y < this.ClaimDetailIDList.Count; y++)
                {
                    if (this.ClaimDetailIDList[y].ToString().CompareTo(this.Table_ClaimDetailGrid.Rows[x].ID) == 0)
                    {
                        this.ClaimDetailIDList.RemoveAt(y);
                    }
                }

                this.Table_ClaimDetailGrid.Rows.RemoveAt(x);
            }
            else
            {
                x++;
            }
        }

        //re-numbering
        for (int i = 1; i < this.Table_ClaimDetailGrid.Rows.Count; i++)
        {
            Label Label_No = (Label)this.Table_ClaimDetailGrid.Rows[i].Cells[1].Controls[0];
            Label_No.Text = i.ToString();
        }

    }
    #endregion

    #region Button_ClearAll_Click
    protected void Button_ClearAll_Click(object sender, EventArgs e)
    {
        this.Table_ClaimDetailGrid.Rows.Clear();
        this.ClaimDetailIDList.Clear();
        this.BuiltTable();
        this.TextBox_TotalCost.Text = "0";


    }
    #endregion

    #region Button_Copy_Click
    protected void Button_Copy_Click(object sender, EventArgs e)
    {
        Session[GonzalesCommonNamingConvention.CurrentClipBoardSession] = this.EntertainmentID;
        this.ShowMessage(this.NameListCopied);
    }
    #endregion

    #region Button_PasteNameList_Click
    protected void Button_PasteNameList_Click(object sender, EventArgs e)
    {
        if (Session[GonzalesCommonNamingConvention.CurrentClipBoardSession] != null &&
            Session[GonzalesCommonNamingConvention.CurrentClipBoardSession].ToString().Length == 36)
        {
            using (ClaimManager CurClaimManager = new ClaimManager(this.CurrentPrivateSession))
            {
                EntertainmentDetails CurNameListDetail = CurClaimManager.GetEntertainmentDetails(Session[GonzalesCommonNamingConvention.CurrentClipBoardSession].ToString());
                CurRow = 1;

                if (CurNameListDetail != null)
                {
                    CurRow = 1;

                    for (int x = (this.Table_ClaimDetailGrid.Rows.Count - 1); x > 0; x--)
                    {
                        HiddenField HiddenField_IsEmpty = (HiddenField)this.Table_ClaimDetailGrid.Rows[x].Cells[0].Controls[1];
                        if (HiddenField_IsEmpty.Value.CompareTo("1") == 0)
                        {
                            // remove from detail id
                            for (int y = 0; y < this.ClaimDetailIDList.Count; y++)
                            {
                                if (this.ClaimDetailIDList[y].ToString().CompareTo(this.Table_ClaimDetailGrid.Rows[x].ID) == 0)
                                {
                                    this.ClaimDetailIDList.RemoveAt(y);
                                    break;
                                }
                            }

                            // remove the empty row
                            this.Table_ClaimDetailGrid.Rows.RemoveAt(x);
                        }
                        else
                        {
                            break;  // break anytime if meet 1 row is not empty
                        }
                    }

                    // remove the emoty row
                    //this.Table_ClaimDetailGrid.Rows.RemoveAt(LastEmptyRowIndex);

                    if (CurNameListDetail.Count > 0)
                    {
                        //if (this.Table_ClaimDetailGrid.Rows.Count > 1)
                        //    CurRow = Convert.ToInt32(this.Table_ClaimDetailGrid.Rows[(this.Table_ClaimDetailGrid.Rows.Count - 1)].Cells[1].Text);
                        //else
                        //    CurRow = 1;

                        for (int x = 0; x < CurNameListDetail.Count; x++)
                        {
                            this.BuildRow();

                            Label Label_No = (Label)this.Table_ClaimDetailGrid.Rows[(this.Table_ClaimDetailGrid.Rows.Count - 1)].Cells[1].Controls[0];
                            if (this.Table_ClaimDetailGrid.Rows.Count > 1)
                                Label_No.Text = Convert.ToString(this.Table_ClaimDetailGrid.Rows.Count - 1);
                            else
                                Label_No.Text = Convert.ToString(this.Table_ClaimDetailGrid.Rows.Count);

                            TextBox TextBox_Attendee = (TextBox)this.Table_ClaimDetailGrid.Rows[(this.Table_ClaimDetailGrid.Rows.Count - 1)].Cells[2].Controls[0];
                            TextBox_Attendee.Text = CurNameListDetail[x].Attendess;

                            TextBox TextBox_Title = (TextBox)this.Table_ClaimDetailGrid.Rows[(this.Table_ClaimDetailGrid.Rows.Count - 1)].Cells[3].Controls[0];
                            TextBox_Title.Text = CurNameListDetail[x].Title;

                            TextBox TextBox_Company = (TextBox)this.Table_ClaimDetailGrid.Rows[(this.Table_ClaimDetailGrid.Rows.Count - 1)].Cells[4].Controls[0];
                            TextBox_Company.Text = CurNameListDetail[x].Company;
                        }

                        // recalculate cost per head
                        int TotalEmptyRow = 0;
                        for (int x = 1; x < this.Table_ClaimDetailGrid.Rows.Count; x++)
                        {
                            TextBox TextBox_Attendee = (TextBox)this.Table_ClaimDetailGrid.Rows[(this.Table_ClaimDetailGrid.Rows.Count - 1)].Cells[2].Controls[0];
                            TextBox TextBox_Title = (TextBox)this.Table_ClaimDetailGrid.Rows[(this.Table_ClaimDetailGrid.Rows.Count - 1)].Cells[3].Controls[0];
                            TextBox TextBox_Company = (TextBox)this.Table_ClaimDetailGrid.Rows[(this.Table_ClaimDetailGrid.Rows.Count - 1)].Cells[4].Controls[0];
                            TextBox TextBox_CostPerHead = (TextBox)this.Table_ClaimDetailGrid.Rows[(this.Table_ClaimDetailGrid.Rows.Count - 1)].Cells[5].Controls[0];

                            if (TextBox_Attendee.Text.Trim().Length == 0 && TextBox_Title.Text.Trim().Length == 0 &&
                                TextBox_Company.Text.Trim().Length == 0 && TextBox_CostPerHead.Text.CompareTo("0") == 9)
                            {
                                TotalEmptyRow++;
                                HiddenField HiddenField_IsEmpty = (HiddenField)this.Table_ClaimDetailGrid.Rows[x].Cells[0].Controls[1];
                                HiddenField_IsEmpty.Value = "1";    // marked empty
                            }
                        }

                        if (decimal.Parse(this.TextBox_TotalCost.Text) > 0)
                        {
                            try
                            {
                                decimal CostPerHead = decimal.Parse(this.TextBox_TotalCost.Text) / (this.Table_ClaimDetailGrid.Rows.Count - 1 - TotalEmptyRow);

                                for (int x = 1; x < this.Table_ClaimDetailGrid.Rows.Count; x++)
                                {
                                    HiddenField HiddenField_IsEmpty = (HiddenField)this.Table_ClaimDetailGrid.Rows[x].Cells[0].Controls[1];
                                    if (HiddenField_IsEmpty.Value.CompareTo("1") != 0)
                                    {
                                        TextBox TextBox_CostPerHead = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[5].Controls[0];
                                        TextBox_CostPerHead.Text = CostPerHead.ToString();
                                    }
                                }

                            }
                            catch (Exception ex)
                            {
                                string LogID = SwissArmy.UniqueID();
                                this.CurLogManager.LogError(LogID, "[GonzalesToolsEntertainment.Button_PasteNameList_Click] : Exception : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                            }

                        }


                    }
                }
                else
                {
                    string LogID = SwissArmy.UniqueID();
                    this.CurLogManager.LogError(LogID, "[GonzalesToolsEntertainment.Button_PasteNameList_Click] : Failed at CurClaimManager.GetEntertainmentDetails : " + CurClaimManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                }

            }

        }

    }
    #endregion

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {
        this.Button_SaveNExit.Attributes["onclick"] = "ReCalCostPerHead();return Validate();";
        this.Button_Done.Attributes["onclick"] = "ReCalCostPerHead();return Validate()";
        this.Button_Remove.Attributes["onclick"] = "return confirm('" + this.ConfirmedMessage + "');";
        this.Button_Calculate.Attributes["onclick"] = "ReCalCostPerHead(); return false;";
        this.Button_PasteNameList.Attributes["onclick"] = "ReCalCostPerHead();";
        this.Button_Submit.Attributes["onclick"] = "ReCalCostPerHead();return Validate();";
        this.Button_Cancel.Attributes["onclick"] = "if (PassParameter()) {parent.ModalWindow.hide(this);}return true;"; //parent.ModalWindow.hide()

        //this.CheckBox_IsGovOfficial.Enabled = false;

        this.TextBox_TotalCost.Text = "0";

        if (this.ReadOnly)
        {
            this.Button_ClearAll.Visible = this.Button_Done.Visible = this.Button_AddMoreRow.Visible = this.Button_Remove.Visible = this.Button_Submit.Visible = this.Button_Calculate.Visible = this.Button_Copy.Visible = this.Button_PasteNameList.Visible = false;
            this.TextBox_Purpose.Attributes["onfocus"] = "this.blur();";
            this.CheckBox_IsGovOfficial.Enabled = false;
        }

        this.TextBox_TotalCost.Attributes["onfocus"] = "TotalOnFocus()";
        this.TextBox_TotalCost.Attributes["onblur"] = this.TextBox_NoOfParticipants.Attributes["onblur"] = "DefaultValue(this, true);";
        //this.RadioButton_EnterNoOfParticpants.Attributes["onClick"] = "SwitchOption(this, document.getElementById('RadioButton_EnterParticpantsName'));";
        //this.RadioButton_EnterParticpantsName.Attributes["onClick"] = "SwitchOption(this, document.getElementById('RadioButton_EnterNoOfParticpants'));";

    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                this.MandatoryMessage = CurLanguageFactory.DisplayValue("MandatoryMessage");
                this.NonNegativeMessage = CurLanguageFactory.DisplayValue("NonNegativeMessage");
                this.WarningMessage = CurLanguageFactory.DisplayValue("WarningMessage");

                this.Label_desc.Text = CurLanguageFactory.DisplayValue("desc");
                this.Label_GovOfficial.Text = CurLanguageFactory.DisplayValue("GovOfficial");
                this.Label_Participants.Text = CurLanguageFactory.DisplayValue("Participants");
                this.Label_ParticipantsDesc.Text = CurLanguageFactory.DisplayValue("ParticipantsDesc");
                this.Label_Title.Text = CurLanguageFactory.DisplayValue("Title");
                this.Label_TotalCost.Text = CurLanguageFactory.DisplayValue("TotalCost");
                this.Label_TotalNoParticipant.Text = CurLanguageFactory.DisplayValue("TotalNoParticipant");
                this.Label2.Text = CurLanguageFactory.DisplayValue("Label2");

                this.Button_AddMoreRow.Text = CurLanguageFactory.DisplayValue("AddMoreRow");
                this.Button_Calculate.Text = CurLanguageFactory.DisplayValue("Calculate");
                this.Button_Cancel.Text = CurLanguageFactory.DisplayValue("Cancel");
                this.Button_ClearAll.Text = CurLanguageFactory.DisplayValue("ClearAll");
                this.Button_Copy.Text = CurLanguageFactory.DisplayValue("Copy");
                this.Button_Done.Text = CurLanguageFactory.DisplayValue("Done");
                this.Button_PasteNameList.Text = CurLanguageFactory.DisplayValue("PasteNameList");
                this.Button_Remove.Text = CurLanguageFactory.DisplayValue("Remove");
                this.Button_SaveNExit.Text = CurLanguageFactory.DisplayValue("SaveNExit");
                this.Button_Submit.Text = CurLanguageFactory.DisplayValue("Submit");

                this.ErrorMessage = CurLanguageFactory.DisplayValue("ErrorMessage");
                this.ConfirmedMessage = CurLanguageFactory.DisplayValue("ConfirmedMessage");
                this.Number = CurLanguageFactory.DisplayValue("Number");
                this.Participants = CurLanguageFactory.DisplayValue("Participants");
                this.Titles = CurLanguageFactory.DisplayValue("Titles");
                this.Affiliates = CurLanguageFactory.DisplayValue("Affiliates");
                this.CostPerHead = CurLanguageFactory.DisplayValue("CostPerHead");
                this.NameListCopied = CurLanguageFactory.DisplayValue("NameListCopied");

                this.DataSaved = CurLanguageFactory.DisplayValue("DataSaved");

            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesToolsTaxi.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #region GenerateWarningMessage
    public void GenerateWarningMessage()
    {
        StringBuilder JSBuilder = new StringBuilder();
        JSBuilder.AppendLine("<script language='javascript'>");
        JSBuilder.AppendLine("var MandatoryMessage = '" + this.MandatoryMessage + "';");
        JSBuilder.AppendLine("var NonNegativeMessage = '" + this.NonNegativeMessage + "';");
        JSBuilder.AppendLine("var WarningMessage = '" + this.NonNegativeMessage + "';");

        JSBuilder.AppendLine("</script>");

        // done, generate on the page
        if (!this.ClientScript.IsClientScriptBlockRegistered("GenerateWarningMessage"))
        {
            this.ClientScript.RegisterClientScriptBlock(Type.GetType("System.String"), "GenerateWarningMessage", JSBuilder.ToString());
        }
    }
    #endregion

    #region BuiltTable

    protected void BuiltTable()
    {
        // add all the table header
        TableRow HeaderRow = new TableRow();
        int CellCounter = 1;

        for (int x = 0; x < this.TotalColumns; x++)
        {
            HeaderRow.Cells.Add(new TableCell());
            HeaderRow.Cells[x].CssClass = "GridViewHeader";
        }

        Image Image_Delete = new Image();
        Image_Delete.ImageUrl = GonzalesImages.Icon_Delete;
        HeaderRow.Cells[0].Controls.Add(Image_Delete);
        HeaderRow.Cells[0].HorizontalAlign = HorizontalAlign.Center;

        HeaderRow.Cells[CellCounter++].Text = this.Number;
        HeaderRow.Cells[CellCounter++].Text = this.Participants;
        HeaderRow.Cells[CellCounter++].Text = this.Titles;
        HeaderRow.Cells[CellCounter++].Text = this.Affiliates;
        HeaderRow.Cells[CellCounter++].Text = this.CostPerHead;

        this.Table_ClaimDetailGrid.Rows.Add(HeaderRow);
    }

    #endregion

    #region BuiltRow

    protected void BuildRow()
    {
        this.BuildRow(SwissArmy.UniqueID());
    }

    protected void BuildRow(string RowID)
    {
        TableRow NewTableRow = new TableRow();
        int CellCounter = 0;

        NewTableRow.ID = RowID;

        if (!this.ClaimDetailIDList.Contains(RowID))
        {
            this.ClaimDetailIDList.Add(NewTableRow.ID); // capture the row id using in the system
        }

        for (int x = 0; x < this.TotalColumns; x++)
        {
            // set default setting to all the row
            NewTableRow.Cells.Add(new TableCell());
            NewTableRow.Cells[x].CssClass = "GridViewItem";
            NewTableRow.Cells[x].HorizontalAlign = HorizontalAlign.Left;
        }

        #region 0. Delete Indicator

        CheckBox CheckBoxDelete = new CheckBox();
        CheckBoxDelete.ID = NewTableRow.ID + "_Delete";
        CheckBoxDelete.Visible = !this.ReadOnly;

        HiddenField HiddenFieldIsEmpty = new HiddenField();
        HiddenFieldIsEmpty.ID = NewTableRow.ID + "_IsEmpty";

        NewTableRow.Cells[CellCounter].Controls.Add(CheckBoxDelete);
        NewTableRow.Cells[CellCounter].Controls.Add(HiddenFieldIsEmpty);
        NewTableRow.Cells[CellCounter].Width = Unit.Pixel(20);

        #endregion

        #region Extra Column to indicate the No.
        Label LabelNo = new Label();
        LabelNo.ID = RowID + "_No";
        LabelNo.Text = CurRow.ToString();
        CurRow++;

        CellCounter++;
        NewTableRow.Cells[CellCounter].Controls.Add(LabelNo);
        NewTableRow.Cells[CellCounter].Width = Unit.Pixel(20);
        #endregion

        #region 1. Attendees

        TextBox TextBox_Attendees = new TextBox();
        TextBox_Attendees.ID = RowID + "TextBox_Attendees";
        TextBox_Attendees.CssClass = "DetailGridTextBox";
        TextBox_Attendees.Width = new Unit(95, UnitType.Percentage);
        TextBox_Attendees.MaxLength = 30;
        //TextBox_Attendees.Attributes["onclick"] = "document.forms[0].RadioButton_EnterParticpantsName.checked = true; document.forms[0].RadioButton_EnterNoOfParticpants.checked = false;";

        if (this.ReadOnly)
        {
            TextBox_Attendees.Attributes["onfocus"] = "this.blur();";
        }

        CellCounter++;
        NewTableRow.Cells[CellCounter].Controls.Add(TextBox_Attendees);
        //NewTableRow.Cells[CellCounter].Width = Unit.Pixel(100);

        #endregion

        #region 2. Title

        TextBox TextBox_Title = new TextBox();
        TextBox_Title.ID = RowID + "TextBox_Title";
        TextBox_Title.CssClass = "DetailGridTextBox";
        TextBox_Title.Width = new Unit(90, UnitType.Percentage);        
        TextBox_Title.MaxLength = 60;
        //TextBox_Title.Attributes["onclick"] = "document.forms[0].RadioButton_EnterParticpantsName.checked = true; document.forms[0].RadioButton_EnterNoOfParticpants.checked = false;";

        if (this.ReadOnly)
        {
            TextBox_Title.Attributes["onfocus"] = "this.blur();";
        }

        CellCounter++;
        NewTableRow.Cells[CellCounter].Controls.Add(TextBox_Title);
        //NewTableRow.Cells[CellCounter].Width = Unit.Pixel(100);

        #endregion

        #region 3. Company
        
        TextBox TextBox_Company = new TextBox();
        TextBox_Company.ID = RowID + "TextBox_Company";
        TextBox_Company.CssClass = "DetailGridTextBox";
        TextBox_Company.Width = new Unit(95, UnitType.Percentage);
        TextBox_Company.MaxLength = 30;
        //TextBox_Company.Attributes["onclick"] = "document.forms[0].RadioButton_EnterParticpantsName.checked = true; document.forms[0].RadioButton_EnterNoOfParticpants.checked = false;";

        if (this.ReadOnly)
        {
            TextBox_Company.Attributes["onfocus"] = "this.blur();";
        }

        CellCounter++;
        NewTableRow.Cells[CellCounter].Controls.Add(TextBox_Company);
        //NewTableRow.Cells[CellCounter].Width = Unit.Pixel(200);

        #endregion

        #region 4. Cost Per Head

        TextBox TextBox_CostPerHead = new TextBox();
        TextBox_CostPerHead.ID = RowID + "TextBox_CostPerHead";
        TextBox_CostPerHead.CssClass = "DetailGridTextBox";
        TextBox_CostPerHead.Style.Add(HtmlTextWriterStyle.TextAlign, "right");
        TextBox_CostPerHead.Width = new Unit(95, UnitType.Percentage);

        if (this.ReadOnly)
        {
            TextBox_CostPerHead.Attributes["onfocus"] = "this.blur();";
        }

        CellCounter++;
        NewTableRow.Cells[CellCounter].Controls.Add(TextBox_CostPerHead);
        NewTableRow.Cells[CellCounter].Width = Unit.Pixel(100);

        #endregion

        // done
        this.Table_ClaimDetailGrid.Rows.Add(NewTableRow);
        this.ClaimDetailRowBuiltCounter++;
    }

    #endregion

    #region ShowErrorMessage
    protected void ShowErrorMessage(string ErrorMessage)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessage;
    }
    #endregion

    #region ShowMessage
    protected void ShowMessage(string Message)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = Message;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "MessageBox";
    }
    #endregion

    #endregion

    #region Data Operation

    #region LoadData
    protected void LoadData()
    {
        using (ClaimManager CurClaimManager = new ClaimManager(this.CurrentPrivateSession))
        {
            ResultObj = CurClaimManager.GetEntertainmentHeader(this.ClaimInternalID, this.ClaimItemId);

            if (ResultObj != null)
            {
                this.EntertainmentID = ResultObj.InternalID;

                // load the items
                this.ResultObj.Details = CurClaimManager.GetEntertainmentDetails(ResultObj.InternalID);
                if (this.ResultObj.Details == null && CurClaimManager.ErrorMessage.Length > 0)
                {
                    string LogID = SwissArmy.UniqueID();
                    this.CurLogManager.LogError(LogID, "[GonzalesToolsEntertainment : LoadData] : CurClaimManager.GetEntertainmentDetails('" + ResultObj.InternalID + "') : " + CurClaimManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                    this.ShowErrorMessage(this.ErrorMessage + LogID);
                }
            }
            else if (CurClaimManager.ErrorMessage.Length > 0)
            {
                // log reasons
                string LogID = SwissArmy.UniqueID();
                this.CurLogManager.LogError(LogID, "[GonzalesToolsEntertainment : LoadData] : CurClaimManager.GetEntertainmentHeader('" + this.ClaimInternalID + "', '" + this.ClaimItemId + "') : " + CurClaimManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                this.ShowErrorMessage(this.ErrorMessage + LogID);
            }
            else
            {
                // this is a new entertainment list
                this.EntertainmentID = SwissArmy.UniqueID();
            }

        }

    }
    #endregion

    #region FillData

    protected void FillData()
    {
        if (this.ResultObj != null)
        {
            this.TextBox_Purpose.Text = this.ResultObj.Purpose;
            this.CheckBox_IsGovOfficial.Checked = this.ResultObj.GovOfficial;
            this.TextBox_TotalCost.Text = this.ResultObj.TotalCost.ToString();
        }
    }

    #endregion

    #region LoadIntoDetailGrid
    protected void LoadIntoDetailGrid()
    {
        int TotalCurrentRow = this.Table_ClaimDetailGrid.Rows.Count - 1;
        int ExtraRowNeeded = (this.ResultObj.Details.Count > TotalCurrentRow) ? (this.ResultObj.Details.Count - TotalCurrentRow) : 0;

        for (int x = 0; x < ExtraRowNeeded; x++)
        {
            // built extra row to cater current claim detail
            this.BuildRow();
        }

        // load into grid now
        for (int x = 1; x < this.Table_ClaimDetailGrid.Rows.Count; x++)
        {
            for (int y = 0; y < this.ResultObj.Details.Count; y++)
            {
                if (this.ResultObj.Details[y].InternalID.CompareTo(this.Table_ClaimDetailGrid.Rows[x].ID) == 0)
                {

                    #region 1. Attendee

                    TextBox TextBox_Attendee = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[2].Controls[0];
                    TextBox_Attendee.Text = this.ResultObj.Details[y].Attendess;

                    #endregion

                    #region 2. Title

                    TextBox TextBox_Title = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[3].Controls[0];
                    TextBox_Title.Text = this.ResultObj.Details[y].Title;

                    #endregion

                    #region 3. Company

                    TextBox TextBox_Company = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[4].Controls[0];
                    TextBox_Company.Text = this.ResultObj.Details[y].Company;

                    #endregion

                    #region 4. Cost Per Head

                    TextBox TextBox_CostPerHead = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[5].Controls[0];
                    TextBox_CostPerHead.Text = this.ResultObj.Details[y].CostPerHead.ToString();

                    #endregion

                    break;
                }
            }

            // cost per head
            TextBox TextBox_CurCostPerHead = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[5].Controls[0];
            if (TextBox_CurCostPerHead.Text.Length == 0)
            {
                TextBox_CurCostPerHead.Text = "0";
            }
        }

        //if (this.ResultObj.Details.Count > 0)
        //{
        //    this.RadioButton_EnterNoOfParticpants.Checked = false;
        //    this.RadioButton_EnterParticpantsName.Checked = true;
        //}

        // remove extra row if this is a read only
        if (this.ReadOnly)
        {
            if ((this.Table_ClaimDetailGrid.Rows.Count - 1) > this.ResultObj.Details.Count)
            {
                int RowToDelete = (this.Table_ClaimDetailGrid.Rows.Count - 1) - this.ResultObj.Details.Count;
                int Counter = 0;

                for (int x = (this.Table_ClaimDetailGrid.Rows.Count - 1); x > 1; x--)
                {
                    Counter++;
                    this.Table_ClaimDetailGrid.Rows[x].Visible = false;

                    if (Counter == RowToDelete)
                    {
                        break;
                    }

                }
            }
        }

    }

    #endregion

    #region DataSave
    protected bool DataSave()
    {
        bool smooth = true;


            using (ClaimManager CurClaimManager = new ClaimManager(this.CurrentPrivateSession))
            {
                if (!(smooth = CurClaimManager.SaveEntertainmentRemarks(this.ResultObj, this.ClaimInternalID, this.ClaimItemId)))
                {
                    string LogID = SwissArmy.UniqueID();
                    this.CurLogManager.LogError(LogID, "[GonzalesToolsEntertainment.SaveData] : Failed at CurClaimManager.SaveEntertainmentRemarks() : " + CurClaimManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                    this.ShowErrorMessage(this.ErrorMessage + LogID);
                }
            }
        

        return smooth;
    }
    #endregion

    #region DataCollect

    protected void DataCollect()
    {
        this.LoadData(); // load data first

        //if (!this.Table_MessageBox.Visible)
        {
            if (this.ResultObj == null)
            {
                // entertainment remarks is optional, hence if dun hav... juz create 1
                // use the one created during begining
                this.ResultObj = new EntertainmentHeader(this.EntertainmentID);
            }

            this.ResultObj.Purpose = this.TextBox_Purpose.Text;
            this.ResultObj.GovOfficial = this.CheckBox_IsGovOfficial.Checked;
            this.ResultObj.Details = new EntertainmentDetails();
            this.ResultObj.TotalCost = decimal.Parse(this.TextBox_TotalCost.Text);

            if (this.Table_ClaimDetailGrid.Rows.Count > 1)
            {
                int OrderCounter = 0;

                for (int x = 1; x < this.Table_ClaimDetailGrid.Rows.Count; x++)
                {
                    TextBox TextBox_Attendee = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[2].Controls[0];
                    TextBox TextBox_Title = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[3].Controls[0];
                    TextBox TextBox_Company = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[4].Controls[0];
                    TextBox TextBox_CostPerHead = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[5].Controls[0];

                    if (TextBox_Attendee.Text.Length > 0 && TextBox_Title.Text.Length > 0 && TextBox_Company.Text.Length > 0)
                    {
                        EntertainmentDetail CurrentDetail = new EntertainmentDetail(this.Table_ClaimDetailGrid.Rows[x].ID);
                        CurrentDetail.Attendess = TextBox_Attendee.Text;
                        CurrentDetail.Title = TextBox_Title.Text;
                        CurrentDetail.Company = TextBox_Company.Text;
                        if (TextBox_CostPerHead.Text.Length > 0)
                            CurrentDetail.CostPerHead = decimal.Parse(TextBox_CostPerHead.Text);
                        else
                            CurrentDetail.CostPerHead = 0;
                        CurrentDetail.Order = OrderCounter++;

                        this.ResultObj.Details.Add(CurrentDetail);
                    }
                }
            }
        }
    }

    #endregion

    #endregion

}
